﻿using AutoMapper;
using ProjectManageSystemApi.DbEntity;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Services.Impl
{
    public class UserPasswordServices : IUserPasswordServices
    {
        readonly IFreeSql _fsql;
        readonly IMapper _mapper;
        readonly Config _config;
        public UserPasswordServices(IFreeSql fsql, IMapper mapper, Config config) 
        {
            this._fsql = fsql;
            this._mapper = mapper;
            this._config = config;
        }
        public bool Create(long id, string userName, string password)
        {
            string pwdMd5 = PasswordEncryption(password);
            var data = new UserPasswordInfo()
            {
                Id = id,
                UserName = userName,
                Password = pwdMd5
            };
            return _fsql.Insert(data).ExecuteAffrows() > 0;
        }
        public bool Update(long id, string password)
        {
            string pwdMd5 = PasswordEncryption(password);
            return _fsql.Update<UserPasswordInfo>(id).Set(x=>x.Password, pwdMd5).ExecuteAffrows() > 0;
        }

        /// <summary>
        /// 密码加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        private string PasswordEncryption(string password) 
        {
            return ApiUtilities.MD5Encrypt(password + _config.PwdSuffix);
        }
    }
}
